package com.openlogic.common.logic.node.parse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.AppBeanLoadEndEvent;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.openlogic.common.logic.node.enums.ParseTypeEnums;
import com.openlogic.common.logic.node.factory.NodeParseFactory;
import com.openlogic.common.logic.utils.NumericRegxUtils;
import com.openlogic.common.logic.utils.OpeatorValidateUtils;

/**
* @ClassName: FieldParse
* @Description: 字段属性节点解析处理器 
* @author weiyongpeng
* @date 2024年7月14日 上午11:50:47
 */
@Component
public class FieldParseHandler extends BaseParseHandler {
	
//	private static final Logger logger = LoggerFactory.getLogger(FieldParseHandler.class);

	@Override
	protected Map<Object, List<?>> setResultMap(JSONObject nodeInfoJsonObject, JSONObject lineInfoJsonObject) {
//		logger.info("开始解析所有Field节点...");
		Map<Object, List<?>> map = new TreeMap<Object, List<?>>();
		// 设置字段含义
		map.put("Name", setFiledName(ParseTypeEnums.FIELD_INFO.getParsetype()));
		// 设置节点Value
		map.put("Value", getAllFiledInfo(nodeInfoJsonObject,lineInfoJsonObject));
		return map;
	}

	@Override
	protected List<List<?>> getAllValueInfo(JSONObject nodeInfoJsonObject, JSONObject lineInfoJsonObject) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private List<Map<String, String>> getAllFiledInfo(JSONObject nodeInfoJsonObject, JSONObject lineInfoJsonObject) {
		List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
		Map<String, String> tmpmap = new HashMap<String, String>();

		Map<String, String> nodeMap = new HashMap<String, String>();
		getValue(nodeInfoJsonObject.toString(), nodeMap); // 处理节点中字段属性

		for (Map.Entry<String, String> entry : nodeMap.entrySet()) { // 处理节点的字段信息
			JSONObject tmp = JSON.parseObject(entry.getValue().toString());
			if (tmp.get("data") == null)
				continue;
			JSONObject tmpObject = JSON.parseObject(tmp.get("data").toString());
			List<String> tmpList = new ArrayList<String>();
			getEveryElementByFiled(tmpObject, tmpList, tmp.get("type").toString(), tmpmap);
		}

		Map<String, String> lineMap = new HashMap<String, String>();
		getValue(lineInfoJsonObject.toString(), lineMap); // 处理节点中字段属性

		for (Map.Entry<String, String> entry : lineMap.entrySet()) { // 处理线条中的字段信息
			JSONObject tmp = JSON.parseObject(entry.getValue().toString());
			if (tmp.get("value") == null)
				continue; // 无value属性
			if (NumericRegxUtils.isNumeric(tmp.get("value").toString())
					|| NumericRegxUtils.isDecimal(tmp.get("value").toString()))
				continue; // value值是数字
			if (!OpeatorValidateUtils.validateIsOp(tmp.get("value").toString()))
				continue; // value值中不包含计算符
			JSONArray tmpArray = JSON.parseArray(tmp.get("value").toString());
			for (int m = 0; m < tmpArray.size(); m++) {
				JSONArray tArray = JSON.parseArray(tmpArray.get(m).toString());
				for (int n = 0; n < tArray.size(); n++) {
					JSONObject tmpObject = (JSONObject) tArray.get(n);
					List<String> tmpList = new ArrayList<String>();
					getEveryElementByFiled(tmpObject, tmpList, "line", tmpmap);

				}
			}
		}
		resultList.add(tmpmap);
		return resultList;
	}
	
	/**
	* 描述：获取各个域中的各个元素值
	* @Title: getEveryElementByFiled
	* @param tmpObject
	* @param tmpList
	* @param type
	* @param tmpmap
	* @return
	* @author weiyongpeng
	* @date  2024年7月15日 下午3:15:10
	 */
	private List<String> getEveryElementByFiled(JSONObject tmpObject, List<String> tmpList, String type,
			Map<String, String> tmpmap) {
		if (!"area".equals(type)) {
			getDataInfoByFiled(tmpObject.toString(), type, tmpmap);
		} else {
			Map<String, String> areaNode = new HashMap<String, String>();
			getValue(tmpObject.toString(), areaNode); // 处理节点中字段属性
			for (Map.Entry<String, String> entry : areaNode.entrySet()) {
				JSONObject tmp = JSON.parseObject(entry.getValue().toString());
				getDataInfoByFiled(tmp.get("data").toString(), tmp.get("type").toString(), tmpmap);
			}
		}
		return tmpList;
	}
	
	/**
	* 描述：拆分节点中的data信息：分为if、success、failed
	* @Title: getDataInfoByFiled
	* @param dataStr
	* @param type
	* @param map
	* @return
	* @author weiyongpeng
	* @date  2024年7月15日 下午3:18:34
	 */
	private Map<String, String> getDataInfoByFiled(String dataStr, String type, Map<String, String> map) {
		JSONObject tmpObject = JSON.parseObject(dataStr);
		if ("switch".equals(type) || "line".equals(type)) {
			String filedName = tmpObject.get("value") == null ? "" : tmpObject.get("value").toString();
			String filedType = tmpObject.get("type") == null ? "" : tmpObject.get("type").toString();
			if (!NumericRegxUtils.isNumeric(filedType))
				filedType = ""; // 如果类型的值不为整型将值设为空
			if (OpeatorValidateUtils.validateIsOp(filedName) || "".equals(filedName) || "null".equals(filedName)
					|| NumericRegxUtils.isNumeric(filedName) || NumericRegxUtils.isDecimal(filedName))
				return map;
			if (map.get(filedName) == null || "".equals(map.get(filedName).toString())) {
				map.put(filedName, filedType);
			}
		} else if ("default".equals(type) || "dec".equals(type) || "matr".equals(type)) {
			String ifValue = tmpObject.get("if") == null ? "" : tmpObject.get("if").toString();
			getEveryFiled(ifValue, map);

			String successValue = tmpObject.get("success") == null ? "" : tmpObject.get("success").toString();
			getEveryFiled(successValue, map);

			String failedValue = tmpObject.get("failed") == null ? "" : tmpObject.get("failed").toString();
			getEveryFiled(failedValue, map);
		} else if ("result".equals(type)) {
			String successValue = tmpObject.get("success") == null ? "" : tmpObject.get("success").toString();
			getEveryFiled(successValue, map);
		} else if ("comm".equals(type)) {
			String successValue = tmpObject.get("success") == null ? "" : tmpObject.get("success").toString();
			getEveryFiled(successValue, map);
		}
		return map;
	}
	
	/**
	* 描述：对if、success、failed各个环节的数据进行拼接
	* @Title: getEveryFiled
	* @param str
	* @param map
	* @author weiyongpeng
	* @date  2024年7月15日 下午3:18:14
	 */
	private void getEveryFiled(String str, Map<String, String> map) {
		JSONArray tmpObject = JSON.parseArray(str);
		for (int i = 0; i < tmpObject.size(); i++) {
			JSONArray tmpArray = tmpObject.getJSONArray(i);
			for (int m = 0; m < tmpArray.size(); m++) {
				JSONObject tmpObj = (JSONObject) tmpArray.get(m);
				String filedName = tmpObj.get("value") == null ? "" : tmpObj.get("value").toString();
				if (OpeatorValidateUtils.validateIsOp(filedName) || "".equals(filedName) || "null".equals(filedName)
						|| NumericRegxUtils.isNumeric(filedName) || NumericRegxUtils.isDecimal(filedName))
					continue;
				String filedType = tmpObj.get("type") == null ? "" : tmpObj.get("type").toString();
				if (!NumericRegxUtils.isNumeric(filedType))
					filedType = ""; // 如果类型的值不为整型将值设为空
				if (map.get(filedName) == null || "".equals(map.get(filedName).toString())) {
					map.put(filedName, filedType);
				}
			}
		}
	}

	@Override
	public void onEvent(AppBeanLoadEndEvent event) throws Throwable {
		NodeParseFactory.register(ParseTypeEnums.FIELD_INFO.getParsetype(), this);
	}

}
